package org.gldap;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.StringTokenizer;

import org.apache.directory.shared.ldap.exception.LdapConfigurationException;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.bouncycastle.jce.provider.JDKGOST3410Signer.ecgost3410;
import org.w3c.dom.Document;


public class Launcher {
	private static EmbeddedADS apds;

	static Logger log4j = Logger.getLogger("org.ldap");
	
	public static void main(String[] args) throws Exception {
		
		log4j.warn("gldap sync lauched");
		log4j.error("gldap sync up");
		ConfFile confFile = new ConfFile();
		GoogleSession a = null;
		try {
			a = new GoogleSession(
					confFile.getConfigrationAttribure("email"), 
					confFile.getConfigrationAttribure("password")
					);
		} catch ( IOException e0){ 
			log4j.error("authentication failed, please verify your login credentials \n " + e0.getMessage());
			throw new Exception(
					"authenticatin failed, please check your login login credentials" + 
					e0.getMessage());
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			log4j.error(e1.getMessage());
		} 
		
		GoogleContacts gc = new GoogleContacts(a);
		String c;
		try {
			c = gc.getContacts();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			log4j.error(  "contacts retrieval failed " + e1.getMessage() );
			throw new Exception( "contacts retrieval failed " );
		} 

		Parser p = new Parser();
		// String k = p.readTextFile("/home/mohammed/python/google/full");
		Document d = p.parseTextIntoDomDocument(c);
		LinkedList<LinkedList> cs = p.parseContacts(d);

		try {
			apds = new EmbeddedADS();
		} catch ( LdapConfigurationException e3){ 
			if ( e3.getMessage().contains("Failed to bind an LDAP service (10389)"))
				throw new Exception("port allready used"+ e3.getMessage() );
			throw e3;
		} catch ( EOFException e3 ){
			log4j.error(e3.getMessage());
			throw new Exception("ldap db corrupted "+ e3.getMessage());
		} catch (Exception e3) {
			// TODO Auto-generated catch block
				log4j.error(e3.getMessage());
				throw new Exception(e3.getMessage());
		}
		
		// first put those contacts/entries in the db
		for (int i = 0; i < cs.size(); i++) {
			// what we want of every contact:
			for (int g = 0; g < cs.get(i).size(); g++) {
				Hashtable m = (Hashtable) cs.get(i).get(g);
				 //System.out.println(m);

				if (m.get("tagName").equals("title") && m.containsKey("nodeValue")) {
					// System.out.println(m.get("nodeValue"));
					String title = m.get("nodeValue").toString();
					log4j.warn("creating title: " + title);
					apds.insertContact(title);
					
					// we have the title of the contact, let us loop in those attributes again:
					for (int u = 0; u < cs.get(i).size(); u++) {
						Hashtable v = (Hashtable) cs.get(i).get(u);
						// primary email
						if (v.get("tagName").equals("gd:email")	&& v.containsKey("primary")) {
							String address = v.get("address").toString();
							log4j.warn("	updating title: " + title + " primary address: " + address);
							apds.addContactAttribute(title, "mail", address);
						}
						// other email
						if (v.get("tagName").equals("gd:email")	&& ! v.containsKey("primary")) {
							String address = v.get("address").toString();
							log4j.warn("	updating title: " + title + " address: " + address);
							apds.addContactAttribute(title, "mail", address);
						}
						// telephone numbers
						if (v.get("tagName").equals("gd:phoneNumber")) {
							String phone = v.get("nodeValue").toString();
							log4j.warn("	updating title: " + title + " mobile: " + phone);
							
							 if ( v.get("rel").equals("http://schemas.google.com/g/2005#mobile") ) 
								 apds.addContactAttribute(title, "mobile", phone);
							 else if ( v.get("rel").equals("http://schemas.google.com/g/2005#work") )
								 apds.addContactAttribute(title, "telephoneNumber", phone);
							 else if ( v.get("rel").equals("http://schemas.google.com/g/2005#other") )
								 apds.addContactAttribute(title, "homePhone", phone); 
							 else 
								 apds.addContactAttribute(title, "facsimileTelephoneNumber", phone); 
						}
						// organization name
						if ( v.get("tagName").equals("gd:organization") ) 
							 apds.addContactAttribute(title, "o", v.get("nodeValue").toString() ); 

					}
				}
			}
		}

		apds.listAllContacts();
	}
}
